package user.view;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import user.dao.UserDao;
import user.db.DBConnection;
import user.model.User;
import user.util.StringUtil;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.ImageIcon;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JPasswordField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.awt.event.ActionEvent;
import java.awt.SystemColor;
import java.awt.Color;

public class RegFrame extends JFrame {

	private JPanel contentPane;
	private JTextField nameText;
	private JPasswordField passwordText;
	private UserDao userDao = new UserDao();
	private JPasswordField rePasswordText;
	private JTextField idText;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					RegFrame frame = new RegFrame();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public RegFrame() {
		setTitle("新用户注册");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 700, 850);
		contentPane = new JPanel();
		contentPane.setBackground(new Color(255, 250, 240));
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		this.setLocationRelativeTo(null);
		setContentPane(contentPane);
		
		ImageIcon icon = new ImageIcon("src/images/background.png");
		JLabel background = new JLabel(icon);
		this.getLayeredPane().setLayout(null);
		contentPane.setOpaque(false);
		background.setBounds(0,0,700,850);
		this.getLayeredPane().add(background,new Integer(Integer.MIN_VALUE));
		
		JLabel Label2 = new JLabel("用户名");
		Label2.setIcon(new ImageIcon(RegFrame.class.getResource("/images/注册登录.png")));
		Label2.setFont(new Font("微软雅黑", Font.PLAIN, 24));
		
		nameText = new JTextField();
		nameText.setFont(new Font("微软雅黑", Font.PLAIN, 24));
		nameText.setColumns(10);
		
		JLabel Label4 = new JLabel("密码");
		Label4.setIcon(new ImageIcon(RegFrame.class.getResource("/images/密码黑.png")));
		Label4.setFont(new Font("微软雅黑", Font.PLAIN, 24));
		
		passwordText = new JPasswordField();
		passwordText.setFont(new Font("宋体", Font.PLAIN, 24));
		
		JButton Button = new JButton("注册");
		Button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				addActionPerformed(e);
			}
		});
		Button.setFont(new Font("宋体", Font.PLAIN, 26));
		Button.setIcon(new ImageIcon(RegFrame.class.getResource("/images/注册.png")));
		
		JLabel Label3 = new JLabel("确认密码");
		Label3.setFont(new Font("微软雅黑", Font.PLAIN, 24));
		
		rePasswordText = new JPasswordField();
		rePasswordText.setFont(new Font("宋体", Font.PLAIN, 24));
		
		JLabel Label1 = new JLabel("学号");
		Label1.setFont(new Font("微软雅黑", Font.PLAIN, 24));
		Label1.setIcon(new ImageIcon(RegFrame.class.getResource("/images/学号管理.png")));
		
		idText = new JTextField();
		idText.setFont(new Font("微软雅黑", Font.PLAIN, 24));
		idText.setColumns(10);
		
		JButton button = new JButton("返回");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				new LoginFrame().setVisible(true);
				dispose();
			}
		});
		button.setIcon(new ImageIcon(RegFrame.class.getResource("/images/返回.png")));
		button.setFont(new Font("宋体", Font.PLAIN, 26));
		
		JButton btnNewButton = new JButton("重置");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				reset();
			}
		});
		btnNewButton.setIcon(new ImageIcon(RegFrame.class.getResource("/images/重置.png")));
		btnNewButton.setFont(new Font("宋体", Font.PLAIN, 26));
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGap(41)
					.addComponent(btnNewButton, GroupLayout.PREFERRED_SIZE, 119, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED, 368, Short.MAX_VALUE)
					.addComponent(button)
					.addGap(39))
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGap(138)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addComponent(Label1)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
								.addComponent(Label2, GroupLayout.PREFERRED_SIZE, 132, GroupLayout.PREFERRED_SIZE)
								.addComponent(Label3)
								.addComponent(Label4))
							.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
								.addGroup(gl_contentPane.createSequentialGroup()
									.addGap(1)
									.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
										.addComponent(passwordText, GroupLayout.DEFAULT_SIZE, 260, Short.MAX_VALUE)
										.addComponent(nameText, GroupLayout.DEFAULT_SIZE, 260, Short.MAX_VALUE)
										.addComponent(idText, GroupLayout.DEFAULT_SIZE, 260, Short.MAX_VALUE)))
								.addGroup(gl_contentPane.createSequentialGroup()
									.addGap(2)
									.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
										.addComponent(Button, GroupLayout.PREFERRED_SIZE, 115, GroupLayout.PREFERRED_SIZE)
										.addComponent(rePasswordText, GroupLayout.DEFAULT_SIZE, 259, Short.MAX_VALUE))))))
					.addGap(141))
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGap(208)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(Label1, GroupLayout.PREFERRED_SIZE, 49, GroupLayout.PREFERRED_SIZE)
						.addComponent(idText, GroupLayout.PREFERRED_SIZE, 46, GroupLayout.PREFERRED_SIZE))
					.addGap(53)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(Label2)
						.addComponent(nameText, GroupLayout.PREFERRED_SIZE, 49, GroupLayout.PREFERRED_SIZE))
					.addGap(73)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(passwordText, GroupLayout.PREFERRED_SIZE, 46, GroupLayout.PREFERRED_SIZE)
						.addComponent(Label4))
					.addGap(56)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(rePasswordText, GroupLayout.PREFERRED_SIZE, 44, GroupLayout.PREFERRED_SIZE)
						.addComponent(Label3))
					.addGap(102)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(btnNewButton, GroupLayout.DEFAULT_SIZE, 61, Short.MAX_VALUE)
						.addComponent(button, GroupLayout.DEFAULT_SIZE, 61, Short.MAX_VALUE)
						.addComponent(Button, GroupLayout.PREFERRED_SIZE, 58, GroupLayout.PREFERRED_SIZE))
					.addGap(52))
		);
		this.setLocationRelativeTo(null);
		contentPane.setLayout(gl_contentPane);
	}

	/**
	 * 对注册事件的处理
	 * @param e
	 */
	protected void addActionPerformed(ActionEvent e) {
		User userMessage = new User();
		String name = nameText.getText().toString();

		String password = new String(passwordText.getPassword());
		String repassword = new String(rePasswordText.getPassword());
		
		
		//注册时，判断输入文本框的值不能为空
		if(StringUtil.isEmpty(idText.getText())) {
			JOptionPane.showMessageDialog(null, "学号不能为空");
			return;
		} 
		
		int id = Integer.parseInt(idText.getText().toString());
		if(StringUtil.isEmpty(name)){
			JOptionPane.showMessageDialog(null, "用户名不能为空！");
			return;
		}
		else if(StringUtil.isEmpty(password)){
			JOptionPane.showMessageDialog(null, "密码不能为空！");
			return;
		}
		//判断两次密码输入是否一致
		if(!password.equals(repassword)) {
			JOptionPane.showMessageDialog(null, "两次输入的密码不一致，请重新输入");
			return;
		}
		//将用户输入的信息封装到userMessage类里面
		userMessage.setUserId(id);
		userMessage.setUserName(name);
		userMessage.setPassword(password);
		
		DBConnection dConnection = new DBConnection();
		UserDao userdao = new UserDao();
		
		//账号不能相同

		try {
			Connection con = dConnection.getConnection();
			int current = userdao.add(con,userMessage);
			
			//提示框，若返回值是0，注册成功
			if(current != 0) {
				JOptionPane.showMessageDialog(null, "注册成功");
				reset();
				return;
			} else {
				JOptionPane.showMessageDialog(null, "账号已存在！");
				return;
			}
		}catch(Exception evt) {
			evt.printStackTrace();
		}
	}

	private void reset() {
		idText.setText("");
		nameText.setText("");
		passwordText.setText("");
		rePasswordText.setText("");
		
	}
}
